home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Diamond Collection / The Diamond Collection (Software Vault)(Digital Impact).ISO / cdr44 / newmat08.zip / NEWMATRM.H < prev    next >
C/C++ Source or Header  |  1995-01-11  |  4KB  |  105 lines

  1. //$$newmatrm.h                            rectangular matrix operations
  2.  
  3. // Copyright (C) 1991,2,3,4: R B Davies
  4.  
  5. #ifndef MATRIXRM_LIB
  6. #define MATRIXRM_LIB 0
  7.  
  8.  
  9. // operations on rectangular matrices
  10.  
  11. class RectMatrixCol;
  12.  
  13. class RectMatrixRowCol
  14. // a class for accessing rows and columns of rectangular matrices
  15. {
  16. protected:
  17.    Real* store;                   // pointer to storage
  18.    int n;                         // number of elements
  19.    int spacing;                   // space between elements
  20.    int shift;                     // space between cols or rows
  21.    RectMatrixRowCol(Real* st, int nx, int sp, int sh)
  22.       : store(st), n(nx), spacing(sp), shift(sh) {}
  23.    void Reset(Real* st, int nx, int sp, int sh)
  24.       { store=st; n=nx; spacing=sp; shift=sh; }
  25. public:
  26.    Real operator*(const RectMatrixRowCol&) const;         // dot product
  27.    void AddScaled(const RectMatrixRowCol&, Real);         // add scaled
  28.    void Divide(const RectMatrixRowCol&, Real);            // scaling
  29.    void Divide(Real);                                     // scaling
  30.    void Negate();                                         // change sign
  31.    void Zero();                                           // zero row col
  32.    Real& operator[](int i) { return *(store+i*spacing); } // element
  33.    Real SumSquare() const;                                // sum of squares
  34.    Real& First() { return *store; }                       // get first element
  35.    void DownDiag() { store += (shift+spacing); n--; }
  36.    void UpDiag() { store -= (shift+spacing); n++; }
  37.    friend void ComplexScale(RectMatrixCol&, RectMatrixCol&, Real, Real);
  38.    friend void Rotate(RectMatrixCol&, RectMatrixCol&, Real, Real);
  39.    FREE_CHECK(RectMatrixRowCol)
  40. };
  41.  
  42. class RectMatrixRow : public RectMatrixRowCol
  43. {
  44. public:
  45.    RectMatrixRow(const Matrix&, int, int, int);
  46.    RectMatrixRow(const Matrix&, int);
  47.    void Reset(const Matrix&, int, int, int);
  48.    void Reset(const Matrix&, int);
  49.    Real& operator[](int i) { return *(store+i); }
  50.    void Down() { store += shift; }
  51.    void Right() { store++; n--; }
  52.    void Up() { store -= shift; }
  53.    void Left() { store--; n++; }
  54.    FREE_CHECK(RectMatrixRow)
  55. };
  56.  
  57. class RectMatrixCol : public RectMatrixRowCol
  58. {
  59. public:
  60.    RectMatrixCol(const Matrix&, int, int, int);
  61.    RectMatrixCol(const Matrix&, int);
  62.    void Reset(const Matrix&, int, int, int);
  63.    void Reset(const Matrix&, int);
  64.    void Down() { store += spacing; n--; }
  65.    void Right() { store++; }
  66.    void Up() { store -= spacing; n++; }
  67.    void Left() { store--; }
  68.    friend void ComplexScale(RectMatrixCol&, RectMatrixCol&, Real, Real);
  69.    friend void Rotate(RectMatrixCol&, RectMatrixCol&, Real, Real);
  70.    FREE_CHECK(RectMatrixCol)
  71. };
  72.  
  73. class RectMatrixDiag : public RectMatrixRowCol
  74. {
  75. public:
  76.    RectMatrixDiag(const DiagonalMatrix& D)
  77.       : RectMatrixRowCol(D.Store(), D.Nrows(), 1, 1) {}
  78.    Real& operator[](int i) { return *(store+i); }
  79.    void DownDiag() { store++; n--; }
  80.    void UpDiag() { store--; n++; }
  81.    FREE_CHECK(RectMatrixDiag)
  82. };
  83.  
  84.  
  85. inline RectMatrixRow::RectMatrixRow
  86.    (const Matrix& M, int row, int skip, int length)
  87.    : RectMatrixRowCol( M.Store()+row*M.Ncols()+skip, length, 1, M.Ncols() ) {}
  88.  
  89. inline RectMatrixRow::RectMatrixRow (const Matrix& M, int row)
  90.    : RectMatrixRowCol( M.Store()+row*M.Ncols(), M.Ncols(), 1, M.Ncols() ) {}
  91.  
  92. inline RectMatrixCol::RectMatrixCol
  93.    (const Matrix& M, int skip, int col, int length)
  94.    : RectMatrixRowCol( M.Store()+col+skip*M.Ncols(), length, M.Ncols(), 1 ) {}
  95.  
  96. inline RectMatrixCol::RectMatrixCol (const Matrix& M, int col)
  97.    : RectMatrixRowCol( M.Store()+col, M.Nrows(), M.Ncols(), 1 ) {}
  98.  
  99. inline Real square(Real x) { return x*x; }
  100. inline Real sign(Real x, Real y)
  101.    { return (y>=0) ? x : -x; }                    // assume x >=0
  102.  
  103.  
  104. #endif
  105.